package cn.stylefeng.guns.modular.cooperationUnit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.file.api.constants.FileConstants;
import cn.stylefeng.roses.kernel.file.api.expander.FileConfigExpander;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.cooperationUnit.entity.CooperationUnit;
import cn.stylefeng.guns.modular.cooperationUnit.enums.CooperationUnitExceptionEnum;
import cn.stylefeng.guns.modular.cooperationUnit.mapper.CooperationUnitMapper;
import cn.stylefeng.guns.modular.cooperationUnit.pojo.request.CooperationUnitRequest;
import cn.stylefeng.guns.modular.cooperationUnit.service.CooperationUnitService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 合作单位表业务实现层
 *
 * @author jizhongbao
 * @date 2022/01/10 15:08
 */
@Service
public class CooperationUnitServiceImpl extends ServiceImpl<CooperationUnitMapper, CooperationUnit> implements CooperationUnitService {

    @Override
    public void add(CooperationUnitRequest cooperationUnitRequest) {
        CooperationUnit cooperationUnit = new CooperationUnit();
        BeanUtil.copyProperties(cooperationUnitRequest, cooperationUnit);
        this.save(cooperationUnit);
    }

    @Override
    public void del(CooperationUnitRequest cooperationUnitRequest) {
        CooperationUnit cooperationUnit = this.queryCooperationUnit(cooperationUnitRequest);
        this.removeById(cooperationUnit.getId());
    }

    @Override
    public void edit(CooperationUnitRequest cooperationUnitRequest) {
        CooperationUnit cooperationUnit = this.queryCooperationUnit(cooperationUnitRequest);
        BeanUtil.copyProperties(cooperationUnitRequest, cooperationUnit);
        this.updateById(cooperationUnit);
    }

    @Override
    public CooperationUnit detail(CooperationUnitRequest cooperationUnitRequest) {
        return this.queryCooperationUnit(cooperationUnitRequest);
    }

    @Override
    public PageResult<Map<String, Object>> findPage(CooperationUnitRequest cooperationUnitRequest) {
        LambdaQueryWrapper<CooperationUnit> wrapper = createWrapper(cooperationUnitRequest);
        wrapper.eq(CooperationUnit::getStatus, 1);
        wrapper.orderByAsc(CooperationUnit::getSort);
        Page<Map<String, Object>> sysRolePage = this.pageMaps(PageFactory.defaultPage(), wrapper);
        for (Map<String, Object> cooperationUnit : sysRolePage.getRecords()) {
            // 获取context-path
            String contextPath = HttpServletUtil.getRequest().getContextPath();
            String logoUrl = FileConfigExpander.getServerDeployHost() + contextPath + FileConstants.FILE_PUBLIC_PREVIEW_URL + "?fileId=" + cooperationUnit.get("logo");
            cooperationUnit.put("logoUrl", logoUrl);
            cooperationUnit.put("linkUrl", cooperationUnit.get("link_url"));
            cooperationUnit.remove("link_url");
        }
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<Map<String, Object>> findList(CooperationUnitRequest cooperationUnitRequest) {
        LambdaQueryWrapper<CooperationUnit> wrapper = this.createWrapper(cooperationUnitRequest);
        wrapper.eq(CooperationUnit::getStatus, 1);
        wrapper.orderByAsc(CooperationUnit::getSort);
        List<Map<String, Object>> cooperationUnits = this.listMaps(wrapper);
        for (Map<String, Object> cooperationUnit : cooperationUnits) {
            // 获取context-path
            String contextPath = HttpServletUtil.getRequest().getContextPath();
            String logoUrl = FileConfigExpander.getServerDeployHost() + contextPath + FileConstants.FILE_PUBLIC_PREVIEW_URL + "?fileId=" + cooperationUnit.get("logo");
            cooperationUnit.put("logoUrl", logoUrl);
            cooperationUnit.put("linkUrl", cooperationUnit.get("link_url"));
            cooperationUnit.remove("link_url");
        }
        return cooperationUnits;
    }

    /**
     * 获取信息
     *
     * @author jizhongbao
     * @date 2022/01/10 15:08
     */
    private CooperationUnit queryCooperationUnit(CooperationUnitRequest cooperationUnitRequest) {
        CooperationUnit cooperationUnit = this.getById(cooperationUnitRequest.getId());
        if (ObjectUtil.isEmpty(cooperationUnit)) {
            throw new ServiceException(CooperationUnitExceptionEnum.COOPERATIONUNIT_NOT_EXISTED);
        }
        return cooperationUnit;
    }

    /**
     * 创建查询wrapper
     *
     * @author jizhongbao
     * @date 2022/01/10 15:08
     */
    private LambdaQueryWrapper<CooperationUnit> createWrapper(CooperationUnitRequest cooperationUnitRequest) {
        LambdaQueryWrapper<CooperationUnit> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = cooperationUnitRequest.getId();
        Long logo = cooperationUnitRequest.getLogo();
        String title = cooperationUnitRequest.getTitle();
        String introduction = cooperationUnitRequest.getIntroduction();
        String linkUrl = cooperationUnitRequest.getLinkUrl();
        Integer sort = cooperationUnitRequest.getSort();
        Integer status = cooperationUnitRequest.getStatus();

        queryWrapper.eq(ObjectUtil.isNotNull(id), CooperationUnit::getId, id);
        queryWrapper.eq(ObjectUtil.isNotNull(logo), CooperationUnit::getLogo, logo);
        queryWrapper.like(ObjectUtil.isNotEmpty(title), CooperationUnit::getTitle, title);
        queryWrapper.like(ObjectUtil.isNotEmpty(introduction), CooperationUnit::getIntroduction, introduction);
        queryWrapper.like(ObjectUtil.isNotEmpty(linkUrl), CooperationUnit::getLinkUrl, linkUrl);
        queryWrapper.eq(ObjectUtil.isNotNull(sort), CooperationUnit::getSort, sort);
        queryWrapper.eq(ObjectUtil.isNotNull(status), CooperationUnit::getStatus, status);

        return queryWrapper;
    }

}